# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.

import sqlite3

def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.

def main():
    dist = {}

    f2 = open('86encode.txt', 'r', encoding = 'utf-8-sig')
    for i in range(97880):
        lst = f2.readline().split('\t')
        if (len(lst) < 2):
            print('error: ', i)
        dist[lst[0]] = lst[1]

    # print(dist['偎'])

    conn = sqlite3.connect('86-wubi-encodes.db')
    cursor = conn.cursor()
    # cursor.execute('''
    #     CREATE TABLE IF NOT EXISTS encodes(shape text, codes text, roots text)
    # ''')
    #
    # cursor.execute('''
    #     DROP TABLE IF EXISTS encodes86
    # ''')

    fout = open('86-wubi-encodes.txt', 'a')

    f1 = open('86.txt', 'r', encoding = 'utf-8-sig')
    for i in range(6861):
        lst = f1.readline().split('\t')
        if (len(lst) != 2):
            print('error: ', i)

        fout.write(f'''{lst[0]}\t{dist[lst[0]].strip()}\t{lst[1].strip()}\n''')

        # print(lst[0], dist[lst[0]].strip(), lst[1].strip())

        # command = f'''
        #     INSERT INTO encodes VALUES('{lst[0]}', '{dist[lst[0]].strip()}', '{lst[1].strip()}')
        # '''

#         command = f'''
#             SELECT COUNT(*) FROM encodes
# '''
#
#         # print(command)
#         cursor.execute(command)
#         print(cursor.fetchone()[0])

    conn.commit()
    conn.close()

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    #print_hi('PyCharm')
    main()

# See PyCharm help at https://www.jetbrains.com/help/pycharm/
